﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QLHREntities;
namespace QLHuongRungDAO
{
    public class MonAnDAO
    {
        private QLHuongRungEntities db = new QLHuongRungEntities();
        public MonAnDAO() { }

        public List<MonAn> LayDanhSachMonAn(int maThucDon)
        {
            var query = from cttd in db.ChiTietThucDons
                        where cttd.ThucDon == maThucDon
                        join ma in db.MonAns on cttd.MonAn equals ma.MaMonAn
                        group ma by ma.MaMonAn into g
                        select new {TenMonAn = g.Select(ma => ma.TenMonAn), MaMonAn =  g.Key };
            List<MonAn> result = new List<MonAn>();
            foreach (var item in query.ToList())
            {
                MonAn ma = new MonAn();
                ma.TenMonAn = item.TenMonAn.First().ToString();
                ma.MaMonAn = item.MaMonAn;
                result.Add(ma);
            }
            return result;
        }

        public MonAn LayMonAn(int maMonAn)
        {
            var query = from ma in db.MonAns
                        where ma.MaMonAn == maMonAn
                        select ma;
            return query.First();
        }
    }
}
